package com.fsgom.gool.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fsgom.common.util.EncryptUtils;
import com.fsgom.common.util.Generics;
import com.fsgom.gool.bean.GoolUserInfo;
import com.fsgom.gool.repository.GoolUserInfoRepository;

@Service
public class GoolUserServiceImpl implements GoolUserService {

	@Autowired
	private GoolUserInfoRepository userInfo;

	@Override
	public GoolUserInfo getUserInfo(Integer userSeq, String userId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userSeq", userSeq);
		params.put("userId", userId);
		return userInfo.selectUserInfo(params);
	}
	
	@Override
	public boolean isOwner(GoolUserInfo userInfo, String reqUserId, String reqPassWd) {
		String userId = userInfo.getUserId();
		if (StringUtils.isEmpty(reqUserId) || !StringUtils.equals(userId, reqUserId)) {
			return false;
		}
		String passWd = userInfo.getUserPasswd();
		if (StringUtils.isEmpty(reqPassWd) || !StringUtils.equals(passWd, reqPassWd)) {
			return false;
		}
		return true;
	}
	
	@Override
	public boolean addUserInfo(String userId, String userNm, String userEmail, String desc, String userPasswd, String userImgUrl) {
		EncryptUtils encUtils = new EncryptUtils();
		String encPasswd = null;
		try {
			encPasswd = encUtils.encrypt(userPasswd);	
			Map<String,	 Object> params = Generics.map();
			params.put("userId", userId);
			params.put("userNm", userNm);
			params.put("userEmail", userEmail);
			userInfo.insertUserInfo(params);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
}
